package com.hrw.musicplayer.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hrw.musicplayer.R;

/**
 * how to write an efficient list adapter. The adapter used in this example
 * binds to an ImageView and to a TextView for each row in the list.
 * 
 * To work efficiently the adapter implemented here uses two techniques: - It
 * reuses the convertView passed to getView() to avoid inflating View when it is
 * not necessary - It uses the ViewHolder pattern to avoid calling
 * findViewById() when it is not necessary
 * 
 * The ViewHolder pattern consists in storing a data structure in the tag of the
 * view returned by getView(). This data structures contains references to the
 * views we want to bind data to, thus avoiding calls to findViewById() every
 * time getView() is invoked.
 */
public class DirectoryAdapter extends BaseAdapter {
	private List<String> list;
	private Bitmap father_file;
	private Bitmap folder_file;
	private Bitmap music_file;
	private LayoutInflater inflater;

	public DirectoryAdapter(Context context, List<String> list) {
		this.list = list;
		inflater = LayoutInflater.from(context);
		music_file = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.itunes2);
		father_file = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.father_file);
		folder_file = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.folder_file);
	}

	@Override
	public int getCount() {

		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// A ViewHolder keeps references to children views to avoid unneccessary
		// calls
		// to findViewById() on each row.
		ViewHolder holder;

		// When convertView is not null, we can reuse it directly, there is no
		// need
		// to reinflate it. We only inflate a new View when the convertView
		// supplied
		// by ListView is null.
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.file_row, null);

			// Creates a ViewHolder and store references to the two children
			// views
			// we want to bind data to.
			holder = new ViewHolder();
			holder.text = (TextView) convertView.findViewById(R.id.text);
			holder.icon = (ImageView) convertView.findViewById(R.id.image_Icon);
			convertView.setTag(holder);
		} else {
			// Get the ViewHolder back to get fast access to the TextView
			// and the ImageView.
			holder = (ViewHolder) convertView.getTag();
		}

		// Bind the data efficiently with the holder.
		if (list.get(position).toString().equals("[...]")) {
			holder.text.setText("[Back]");
			holder.icon.setImageBitmap(father_file);
			return convertView;
		}
		if (list.get(position).indexOf(".mp3") != -1) {
			holder.text.setText(list.get(position));
			holder.icon.setImageBitmap(music_file);
			return convertView;
		} else {
			if (list.get(position).indexOf("/sdcard") != -1
					|| list.get(position).indexOf("/system") != -1) {
				holder.text.setText(list.get(position));
				holder.icon.setImageBitmap(folder_file);
			}
			return convertView;
		}
	}

	static class ViewHolder {
		private TextView text;
		private ImageView icon;
	}
}
